package com.dragon.app.controller.finance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.app.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.finance.IPaymentService;
import com.dragon.repository.domain.dto.PaymentDTO;
import com.dragon.repository.domain.vo.PaymentVO;
import com.dragon.repository.entity.Payment;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 付款 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-11-08
 */
@RestController
@RequestMapping("payment")
@Api(tags = "付款")
public class PaymentController {

    @Autowired
    IPaymentService service;

    @GetMapping("list")
    @ApiOperation("获取付款列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = false, dataType = "String"),
            @ApiImplicitParam(name = "paymentStatus", value = "收款状态", required = false, dataType = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, type = "string"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, type = "string")
    })
    public ResponseData<List<PaymentDTO>> reimburseList(@RequestParam Integer page, @RequestParam Integer limit,
                                                        @RequestParam(required = false) String search,
                                                        @RequestParam(required = false) Integer status,
                                                        @RequestParam(required = false) Integer paymentStatus,
                                                        @RequestParam(required = false) String startTime,
                                                        @RequestParam(required = false) String endTime){
        Page<Payment> payments = service.paymentList(page, limit, search, status, paymentStatus, startTime, endTime);
        return ResponseData.success(PaymentDTO.convertList(payments.getRecords()), payments.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("新增")
    @AddPage
    public ResponseData addPayment(@RequestBody @Validated(Insert.class) PaymentVO vo) {
        return ResponseData.success(service.addPayment(PaymentVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public ResponseData updatePayment(@RequestBody @Validated(Update.class) PaymentVO vo) {
        service.updatePayment(PaymentVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("paymentOperate")
    @ApiOperation("确认付款")
    public ResponseData PaymentOperate(@RequestBody @Validated(Update.class) PaymentVO vo) {
        service.paymentOperate(PaymentVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "付款id", required = true, type = "int"),
    })
    public ResponseData deletePayment(@RequestParam Integer id){
        service.deletePayment(id);
        return ResponseData.success();
    }

    @GetMapping("listById")
    @ApiOperation("根据id查看详情")
    public ResponseData<PaymentDTO> getPaymentById( @RequestParam
                                     @ApiParam(name = "id", value = "付款主表id", required = true, type = "int") Integer id){
        Payment payment = service.getPaymentById(id);
        return ResponseData.success(PaymentDTO.convert(payment));
    }

}
